package com.system.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.system.data.api.IUserDataAccess;
import com.system.data.entity.User;
import com.system.data.util.DataConnection;

public class UserDataAccess implements IUserDataAccess{

	public final String LOGIN_QUERY_STRING = "SELECT * FROM {} WHERE {} = ?";
	
	public User getUserWithUsername(final String username) throws SQLException {
		Connection conn = null;
		User user = null;
		PreparedStatement pst = null;
		try {
			conn = new DataConnection().getConnection();
			pst = conn.prepareStatement(LOGIN_QUERY_STRING);
			pst.setString(1, username);
			final ResultSet rs = pst.executeQuery();
			if(rs.next()){
				user = new User();
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
			}
		} catch (ClassNotFoundException e) {
			//TODO
		} catch (SQLException e) {
			if(pst != null && !pst.isClosed()){
				pst.close();
			}
			//TODO
		} finally {
			if(conn != null && !conn.isClosed()){
				conn.close();
			}
		}
		return user;
	}

}
